package templates

var gormTemplate = `
package {{param "packageName"}}
{{ if .Imports }}
import (
{{- range .Imports}}
	"{{.}}"
{{- end}}
)
{{end}}

{{$structName := CamelizeStr .Name true}}

type {{$structName}} struct {
{{- range .Columns}}
	{{CamelizeStr .Name true}} {{.GoType}} {{.Tag}}
{{- end}}
}
{{$firstChar := FirstCharacter .Name}}
// TableName table name
func ({{$firstChar}} *{{$structName}}) TableName() string {
	return "{{.Name}}"
}

func {{$structName }}AllList() (*[]{{$structName}}, error) {
	var u []{{$structName}}
	err := dao.DB.Find(&u).Error
	if err != nil {
		return nil, fmt.Errorf("查询失败%s", err.Error())
	}
	return &u, nil
}

func {{$structName}}List(t *{{$structName}}, page, size int) (*[]{{$structName}}, int64, error) {
	var list []{{$structName}}
	limit, offset := utils.Page(page, size)
	err := dao.DB.Where(t).Limit(limit).Offset(offset).Find(&list).Error
	if err != nil {
		return nil, 0, err
	}
	total, err := {{$structName}}Total(t)
	if err != nil {
		return nil, 0, err
	}
	return &list, total, nil
}

func {{$structName}}Total(t *{{$structName}}) (total int64, err error) {
	var list []{{$structName}}
	err = dao.DB.Where(t).Find(&list).Count(&total).Error
	if err != nil {
		return 0, err
	}
	return total, nil
}

func Get{{$structName}}(t *{{$structName}}) (*{{$structName}}, error) {
	if err := dao.DB.Where(&t).Find(&t).Error; err != nil {
		return nil, err
	}
	return t, nil
}

func Add{{$structName}}(t *{{$structName}}) (*{{$structName}}, error) {
	if l, _ := Get{{$structName}}(t); l != nil {
		return nil, fmt.Errorf("数据已存在！")
	}
	if err := dao.DB.Create(&t).Error; err != nil {
		return nil, fmt.Errorf("添加失败！")
	}
	return t, nil
}

func Edit{{$structName}}(t *{{$structName}}) (bool, error) {
	err := dao.DB.Model(&t).Updates(&{{$structName }}{}).Error
	if err != nil {
		return false, err
	}
	return true, nil
}

func Delete{{$structName}}(t *{{$structName}}) (bool, error) {

	return true, nil
}
`
